package com.wsc.wisdomchinaqinghe.activitity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;


public class ChangePhoneNumActivity extends BaseActivity implements View.OnClickListener,ContributorView{
    private TextView submit_chang_phone_num,tv_headtitle;
    private Button find_code;
    private ImageView regist_back;
    private EditText sms_code, new_phone_num;
    private TimeCount registerCheck;
    private ContributorPresenter contributorPresenter;
    private Context context;
    private MyToast myToast;
    private final String USER_SEND_VCODE_FLAG="USER_SEND_VCODE_FLAG";
    private final String USER_JUDGE_VCODE_FLAG="USER_JUDGE_VCODE_FLAG";
    private final String USER_UPDATE_PHONE_FLAG="USER_UPDATE_PHONE_FLAG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_phonenum_layout);
        context=this;
        myToast=new MyToast(context);
        initView();
        registerCheck = new TimeCount(60000, 1000);
        contributorPresenter=new ContributorPresenter(this,context);
    }

    private void initView() {
        submit_chang_phone_num = (TextView) findViewById(R.id.tv_submit);
        regist_back = (ImageView) findViewById(R.id.regist_back);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("修改手机号");
        find_code = (Button) findViewById(R.id.find_code);
        new_phone_num = (EditText) findViewById(R.id.new_phone_num);
        sms_code = (EditText) findViewById(R.id.sms_code);
        regist_back.setOnClickListener(this);
        find_code.setOnClickListener(this);
        submit_chang_phone_num.setOnClickListener(this);
        submit_chang_phone_num.setText("提交");
    }


    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //发送验证码
            if (flag.equals(USER_SEND_VCODE_FLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("10400"))
                {
                    myToast.showShort("用户不存在");
                }else if(code.equals("10502"))
                {
                    myToast.showShort("该手机号已经注册过");
                }else if(code.equals("10600"))
                {
                    myToast.showShort("验证码发送失败");
                }else if(code.equals("10601"))
                {
                    myToast.showShort("验证码发送频繁");
                }
                else if(code.equals("10700"))
                {
                    myToast.showShort("操作失败");
                }else if(code.equals("200"))
                {
                    Object message=respObj.getMessage();
                    if(message.equals("21"))
                        myToast.showShort("验证码发送正确");
                }
            }
            else if(flag.equals(USER_JUDGE_VCODE_FLAG))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("10602"))
                {
                    myToast.showShort("验证码失效");
                }else if(code.equals("10700"))
                {
                    myToast.showShort("操作失败");
                } else if(code.equals("200"))
                {
                    Object message=respObj.getMessage();
                    if(message.equals("22"))
                    {
                        String uid= Tools.getSharedPreferencesStrings(Constant.SharePreferenceUserid,"",context);
                        String phone=new_phone_num.getText().toString().trim();
                        contributorPresenter.getUpdatePhone(uid,phone,USER_UPDATE_PHONE_FLAG);
                    }
                }
            }else if(flag.equals(USER_UPDATE_PHONE_FLAG))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                 if(code.equals("10700"))
                {
                    myToast.showShort("操作失败");
                } else if(code.equals("200"))
                {
                    Object message=respObj.getMessage();
                    if(message.equals("41"))
                    {
                        Intent intent=getIntent();
                        intent.putExtra("phone",new_phone_num.getText().toString().trim());
                        setResult(0,intent);
                        finish();
                    }
                }
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error,String flag) {

    }

    /* 定义一个倒计时的内部类 */
    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            find_code.setText("重获验证码");
            setSendRegisterCodeEnable();
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            find_code.setText(millisUntilFinished / 1000 + "秒后发送");
            setSendRegisterCodeUnenable();
        }
    }
    private void setSendRegisterCodeEnable() {
        find_code.setClickable(true);
        find_code.setBackgroundResource(R.drawable.ok_shape);
    }
    private void setSendRegisterCodeUnenable() {
        find_code.setClickable(false);
        find_code.setBackgroundResource(R.drawable.ok_shape_click);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.regist_back:
                finish();
                break;
            case R.id.find_code:
                if (new_phone_num.getText().toString().trim().isEmpty()) {
                    Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!Tools.isMobile(new_phone_num.getText().toString())) {
                    Toast.makeText(this, "请填写正确的手机号", Toast.LENGTH_SHORT).show();
                }else
                {
                    registerCheck.start();
                    contributorPresenter.getUserSendVcode(new_phone_num.getText().toString().trim(),0,USER_SEND_VCODE_FLAG);
                }
                break;
            case R.id.tv_submit:
                String code = sms_code.getText().toString().trim();
                String phone=new_phone_num.getText().toString().trim();
                if (phone.isEmpty()) {
                    Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (code.isEmpty()) {
                    Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                contributorPresenter.getUserJudgeVcode(phone,code,USER_JUDGE_VCODE_FLAG);
                break;
        }
    }

}
